<!-- 护理等级列表页 -->
<template>
  <div class="min-h serveGrade bg-wt">
    <!-- 组件整合，其他组件需要定义在这个div内部 -->

    <!-- 搜索组件 -->
     <SearchForm
      :searchData="pagination"
      @handleSearch="handleSearch"
      @handleReset="handleReset"
     />

    <!-- 表格组件 -->
     <TableList 
      @handleForbidden="handleDisableDialog"
      @handleClickDelete="handleDeleteDialog"
      @handle-bulid="handleBulid"
      :list-data="listData"
      :total="total"
      :pagination="pagination"
      :dataLoading="dataLoading"
      @handle-edit="openEdit"
     />

     <!-- 新增弹框组件 -->
      <DialogForm
        ref = "formRef"
        :visible="visible"
        :title="title"
        :data="DialogFormdata"
        :planData="planList"
        @handleClose="handleClose"
        @handleAdd="handleAdd"
        @handleEditForm = "editNursingProject"
       />

       <!-- 删除弹窗子组件 -->
       <DeleteDialog
        @handle-delete="handleDelete"
        @handle-close="handleDeleteDialogClose"
        :visible="deleteDialogvisible"
        />

        <!-- 禁用弹框子组件 -->
         <DisableDialog
         @handle-click="handleDisable"
         @handle-close="handleDisableDialogClose"
         :visible="disableDialogvisible"
         />
    
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue';
import TableList from './components/TableList.vue';
import SearchForm from './components/SearchForm.vue';
import DialogForm from './components/DialogFrom.vue';
import { getLevelDetails, getLevelList, getnursAllPlanList, levelAdd, levelDelete, levelStatus, levelUpdate } from '@/api/serve';
import DeleteDialog from '@/components/OperateDialog/index.vue'
import DisableDialog from '@/components/OperateDialog/disable.vue'
import { MessagePlugin } from 'tdesign-vue-next';

// 删除弹窗显示隐藏
const deleteDialogvisible = ref(false);
// 禁用弹窗显示隐藏
const disableDialogvisible = ref(false);

const listData = ref([]);
const total = ref(0);
const visible = ref(false);
const title = ref('');
const planList = ref([])
const DialogFormdata = ref(null)
const formRef = ref(null);
const grandId = ref(null);
const grade = ref(null);

//打开删除弹框
const handleDeleteDialog = (val) => {
  debugger;
  grandId.value = val.id;
  deleteDialogvisible.value = true;
}
// 关闭删除弹框
const handleDeleteDialogClose = () => {
  debugger;
  deleteDialogvisible.value = false;
}
/* 删除项目 */
const handleDelete = async () => {
  debugger;
  const res = await levelDelete(grandId.value);
  if (res.code === 200) {
    MessagePlugin.success('删除成功')
    pageQuery()
    handleDeleteDialogClose()
  } else {
    MessagePlugin.error(res.msg)
  }
}

/* 打开禁用弹框 */
const handleDisableDialog = (val) => {
  debugger;
  grade.value = val;
  disableDialogvisible.value = true;
}
/* 关闭禁用弹框 */
const handleDisableDialogClose = () => {
  debugger;
  disableDialogvisible.value = false;
}

/* 禁用项目 */
const handleDisable = async () => {
  debugger;
  const res = await levelStatus(grade.value);
  if (res.code === 200) {
    MessagePlugin.success('禁用成功')
    pageQuery()
    handleDisableDialogClose()
  } else {
    MessagePlugin.error(res.msg)
  }
}



// 分页
const pagination = ref<Object | any>({
  pageSize: 10,
  pageNum: 1 // 默认当前页
})
const dataLoading = ref(false);

const editNursingProject = async (val)=> {
  const result = await levelUpdate(val);
  if (result.code === 200) {
    MessagePlugin.success('编辑成功')
    handleClose()
    pageQuery()
    formRef.value.handleClear()
  }else{
    MessagePlugin.error(result.msg);
  }
}
const openEdit = async (row: any) => {
  visible.value = true;
  title.value = '编辑';
  const result = await getLevelDetails(row.id);
  if (result.code === 200) {
    DialogFormdata.value = result.data;
  }
}
const handleReset = () => {
  delete pagination.value.name;
  delete pagination.value.status;
  pageQuery();
}

const handleSearch = () => {
  pagination.value.pageNum = 1;
  pageQuery();
}
const handleBulid = () => {
    // 显示弹窗
    title.value = '新增'
    visible.value = true
}
const pageQuery = async () => {
  const result = await getLevelList(pagination.value);
  if (result.code === 200) {
    debugger
    listData.value = result.data.records;
    total.value = result.data.total;
  }
}

const queryPlan = async () => {
  const result = await getnursAllPlanList();
  if (result.code === 200) {
    planList.value = result.data;
  }
}

const handleClose = () => {
  visible.value = false
}

const handleAdd = async (val) => {
  const result = await levelAdd(val);
  if (result.code === 200) {
    MessagePlugin.success('添加成功')
    handleClose()
    pageQuery()
    formRef.value.handleClear()
  }else{
    MessagePlugin.error(result.msg);
  }
}

onMounted(() => {
  queryPlan();
  pageQuery();
})

</script>